package com.shicijiaoliu.controller;

import com.shicijiaoliu.pojo.Replypost;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Mainpostservice;
import com.shicijiaoliu.service.Replyservice;
import com.shicijiaoliu.service.Sectionservice;
import com.shicijiaoliu.service.Userservice;
import com.shicijiaoliu.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rpost")
@Validated
public class replypostcontroller {
    @Autowired
    private Replyservice replyservice;

    @Autowired
    private Userservice userservice;

    @Autowired
    private Mainpostservice mainpostservice;

    @Autowired
    private Sectionservice sectionservice;

    @GetMapping("/list")
    public result<Object> list(int countpage, int pagenumber,
                               @RequestParam(required = false) String username,
                               @RequestParam(required = false) String mpostname){
        long aId = 0; long mId = 0;
        if(username!=null){
            if(userservice.findByName(username) == null){
                return result.error("用户不存在，请重新搜索");
            }else{
                aId = userservice.findByName(username).getId();
            }
        }
        if(mpostname!=null){
            if(mainpostservice.findBytitle(mpostname) == null){
                return result.error("主贴不存在，请重新搜索");
            }else{
                mId = mainpostservice.findBytitle(mpostname).getMId();
            }
        }
        List<Replypost> replypostlist = replyservice.findBypage(countpage,pagenumber,aId,mId);
        for(int i = 0;i < replypostlist.size();i++){
            String uname = userservice.findByid(replypostlist.get(i).getAId()).getUsername();
            String mname = mainpostservice.findByid(replypostlist.get(i).getMId()+1).getTitle();
            replypostlist.get(i).setUsername(uname);
            replypostlist.get(i).setMpostname(mname);
        }
        if(replypostlist == null){
            return result.error("获取页大于总页数");
        }else{
            int count = replyservice.getcount(aId,mId);
            Map<String,Object> data = new HashMap<>();
            data.put("total",count);
            data.put("items",replypostlist);
            return result.success(data);}
    }



    @DeleteMapping("/del/{id}")
    public result del(@PathVariable("id")long rId){
        if (replyservice.findByid(rId) == null) {
            return result.error("没有找到该主贴");
        } else {
            replyservice.delByid(rId);
            return result.success();
        }
    }

    @PatchMapping("/jia/{id}")
    public result jia(@PathVariable("id")long rId){
        if (replyservice.findByid(rId) == null) {
            return result.error("没有找到该主贴");
        } else {
            replyservice.jiajing(rId);
            return result.success();
        }
    }

    @GetMapping("/rposts/{mid}")
    public result<Object> rpost(@PathVariable("mid")long mid){

        List<Replypost> rposts = replyservice.findBymid(mid);
        for(int i = 0;i < rposts.size();i++){
            String uname = userservice.findByid(rposts.get(i).getAId()).getUsername();
            String mname = mainpostservice.findByid(rposts.get(i).getMId()).getTitle();
            rposts.get(i).setUsername(uname);
            rposts.get(i).setMpostname(mname);
        }
        long clicks = 0;
        for (Replypost x : rposts){
            clicks += x.getClicks();
        }
        Map<String,Object> data = new HashMap<>();
        data.put("data",rposts);
        data.put("num",rposts.size());
        data.put("clicks",clicks);
        return result.success(data);

    }

    @PostMapping("/add")
    public result add(String content,long mid){
        LocalDateTime time = LocalDateTime.now();
        Map<String,Object> claims = ThreadLocalUtils.getThreadLocal();
        long aid = Long.parseLong((String) claims.get("id"));
        Replypost rpost = new Replypost(time,content,mid,aid);
        replyservice.add(rpost);
        return result.success();
    }
}
